﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using SMARTHOTEL.hotel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace SMARTHOTEL.RoomTypes
{

    /// <summary>
    /// 房间类型管理
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class RoomTypeService : ApplicationService, IRoomTypeService
    {

        private readonly IRepository<RoomType> repository;


        public RoomTypeService(IRepository<RoomType> repository)
        {
            this.repository = repository;
        }


        /// <summary>
        /// 添加  
        /// </summary>
        /// <param name="roomTypeDto"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> CreateAllAsync(CreateUpdateRoomTypeDto roomTypeDto)
        {
            try
            {
                RoomType roomType = ObjectMapper.Map<CreateUpdateRoomTypeDto, RoomType>(roomTypeDto);

                var roomTypeList = (await repository.GetListAsync()).FirstOrDefault(x=>x.RoomTypeName==roomTypeDto.RoomTypeName);

                if (roomTypeList == null)
                {
                    await repository.InsertAsync(roomType);
                    return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Message = "添加成功" };
                }
                else
                {
                    return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "添加失败，房型名称已存在！" };
                }
            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "添加失败"+ex.Message };
            }
        }


        /// <summary>
        /// 显示        
        /// </summary>
        /// <param name="roomTypeDto"></param>
        /// <returns></returns>
        public async Task<ResultDto<List<RoomTypeDto>>> GetAllAsync(string? roomTypeName)
        {
            var roomTypes = await repository.GetListAsync(x=>!x.IsDeleted);
            if (roomTypeName != null)
            {
                roomTypes = roomTypes.Where(x => x.RoomTypeName.Contains(roomTypeName)).ToList();
            }
            return new ResultDto<List<RoomTypeDto>> { Data = ObjectMapper.Map<List<RoomType>, List<RoomTypeDto>>(roomTypes), Code = StatusCode.Success, Message = "查询成功" };
        }
        /// <summary>
        /// 批量删除房间类型（也可以单个删除）
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> DeleteAllAsync(string Id)
        {
            try
            {
                var ids = Id.Split(",");
                foreach (var item in ids)
                { 
                    var roomtype = await repository.GetAsync(x => x.Id == Guid.Parse(item));
                    if (roomtype != null)
                    {
                        roomtype.IsDeleted = true; 
                        await repository.UpdateAsync(roomtype);
                    }
                }
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "删除成功"  };

            }
            catch (Exception ex)
            {
                return new ResultDto<bool> { Data = false, Code = StatusCode.Faield, Message = "删除失败"+ex.Message };
            }
        }
    }
}
